package com.miyouke.chat.exception;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 统一返回结果类
 *
 * @author cheer
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

    private int code;

    private String message;

    private T data;

    public static <T> Result<T> success() {
        return new Result<>(ResultEnum.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultEnum.SUCCESS, data);
    }

    public static <T> Result<T> successMessage(String message) {
        return new Result<>(ResultEnum.SUCCESS, message);
    }

    public static <T> Result<T> error(ResultEnum resultEnum) {
        return new Result<>(resultEnum);
    }

    public static <T> Result<T> error(ResultEnum resultEnum, String message) {
        return new Result<>(resultEnum, message);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(ResultEnum.SERVER_ERROR, message);
    }

    @JsonIgnore
    public boolean isSuccess() {
        return ResultEnum.SUCCESS.getCode().equals(code);
    }

    private Result(ResultEnum result) {
        this.code = result.getCode();
        this.message = result.getMessage();
    }

    private Result(ResultEnum result, String message) {
        this(result);
        if(StrUtil.isNotBlank(message)){
            this.message = message;
        }
    }

    public Result(ResultEnum result, T data) {
        this.code = result.getCode();
        this.message = result.getMessage();
        this.data = data;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

}
